package com.ceair.flight.refund.service.appmobile.impl;

import com.ceair.flight.refund.dto.appmoblie.AddInfo;
import com.ceair.flight.refund.dto.appmoblie.AuditingInfo;
import com.ceair.flight.refund.dto.appmoblie.CouponInfo;
import com.ceair.flight.refund.dto.appmoblie.DruidPaxInfo;
import com.ceair.flight.refund.dto.appmoblie.DruidPayInfo;
import com.ceair.flight.refund.dto.appmoblie.DruidSegInfo;
import com.ceair.flight.refund.dto.appmoblie.EnclosureInfo;
import com.ceair.flight.refund.dto.appmoblie.OrderDetailSearchReq;
import com.ceair.flight.refund.dto.appmoblie.OrderDetailSearchRes;
import com.ceair.flight.refund.dto.appmoblie.RefundApplyDeputyDto;
import com.ceair.flight.refund.dto.appmoblie.RefundApplyDto;
import com.ceair.flight.refund.dto.appmoblie.RefundAttachmentDto;
import com.ceair.flight.refund.dto.appmoblie.RefundAuditDto;
import com.ceair.flight.refund.dto.appmoblie.RefundSegDto;
import com.ceair.flight.refund.dto.appmoblie.RefundTktDto;
import com.ceair.flight.refund.dto.appmoblie.RelateOrderDetail;
import com.ceair.flight.refund.dto.appmoblie.ReportOrderDto;
import com.ceair.flight.refund.dto.appmoblie.ReturnNoteAirInfo;
import com.ceair.flight.refund.dto.appmoblie.ReturnNoteInfo;
import com.ceair.flight.refund.dto.appmoblie.ReturnNoteOrderInfo;
import com.ceair.flight.refund.dto.appmoblie.ReturnNotePaxInfo;
import com.ceair.flight.refund.dto.appmoblie.ReturnNotePayInfo;
import com.ceair.flight.refund.dto.appmoblie.ReturnNoteSearchReq;
import com.ceair.flight.refund.dto.appmoblie.ReturnNoteSearchRes;
import com.ceair.flight.refund.dto.appmoblie.ReturnNoteSegInfo;
import com.ceair.flight.refund.dto.appmoblie.SubOrderInfo;
import com.ceair.flight.refund.dto.external.RefundableResult;
import com.ceair.flight.refund.dto.external.RefundableResultList;
import com.ceair.flight.refund.dto.ota.RefundListDto;
import com.ceair.flight.refund.dto.ota.RefundListReq;
import com.ceair.flight.refund.dto.ota.RefundListRes;
import com.ceair.flight.refund.location.dao.IFlightDao;
import com.ceair.flight.refund.location.dao.IFlightOrderDao;
import com.ceair.flight.refund.location.dao.IOrderDao;
import com.ceair.flight.refund.location.dao.IPaymentMistakeDao;
import com.ceair.flight.refund.location.dao.IRefundApplyDeputyDao;
import com.ceair.flight.refund.location.dao.IRefundDetailDao;
import com.ceair.flight.refund.location.dt.dto.AncillaryConstant;
import com.ceair.flight.refund.location.dt.enums.FlightOrderStatusEnum;
import com.ceair.flight.refund.location.model.FlightOrderDO;
import com.ceair.flight.refund.location.model.OrderDO;
import com.ceair.flight.refund.location.model.PaxDO;
import com.ceair.flight.refund.location.model.PayDO;
import com.ceair.flight.refund.location.model.PayProductDO;
import com.ceair.flight.refund.location.model.SegmentDO;
import com.ceair.flight.refund.location.model.TaxFeeDO;
import com.ceair.flight.refund.location.model.TicketDO;
import com.ceair.flight.refund.location.service.IRefundLocationService;
import com.ceair.flight.refund.service.appmobile.IAppRefundDetailService;
import com.ceair.flight.refund.util.Constants;
import com.ceair.flight.refund.util.DateUtil;
import com.ceair.flight.refund.util.GsonUtil;
import com.ceair.muservice.baggage.dto.EmdBageOrderListDto;
import com.ceair.muservice.basicdata.dto.airport.AirportDto;
import com.ceair.muservice.basicdata.dto.channel.ChannelManageDto;
import com.ceair.muservice.emd.dto.AncillaryListRes;
import com.ceair.muservice.emd.dto.AncillaryOrderListReq;
import com.ceair.muservice.emd.dto.EMDOrderDto;
import com.ceair.muservice.emd.feign.client.IFnEmd;
import com.ceair.muservice.gaotie.dto.GaotieOrderListRes;
import com.ceair.muservice.wifi.dto.WifiOrderDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

@Service("appRefundDetailService")
@Path("/")
public class AppRefundDetailServiceImpl implements IAppRefundDetailService {
	private static final Logger LOG = LoggerFactory.getLogger(AppRefundDetailServiceImpl.class);
	@Autowired
	private IPaymentMistakeDao paymentMistakeDao;
	@Autowired
	private IFlightOrderDao flightOrderDao;
	@Autowired
	private IRefundDetailDao refundDetailDao;
	@Autowired
	IFlightDao flightDao;
	@Autowired
	IRefundLocationService refundLocationService;
	@Autowired
	private IOrderDao orderDao;
	@Autowired
	private IFnEmd fnEmd;
	@Autowired
	private IRefundApplyDeputyDao refundApplyDeputyDao;

	@Override
	@POST
	@Path("/refundList")
	@Consumes({ "application/json" })
	@Produces(MediaType.APPLICATION_JSON)
	public RefundListRes refundList(RefundListReq request) {
		LOG.info("调用入参 : " + GsonUtil.toJson(request));
		// 审核拒绝:252; 等待审核:251; 等待退款:254; 等待重新审核:253; 完成退款:255; OTA

		// 退票状态(101:提交申请, 109:退票异常, 201:等待审核(等待一审), 2011:等待用户更新, 209:一审异常, 301:等待二审,
		// 3011:等待重新审核, 302:二审通过, 309:生成退票单号异常, 401:等待退款, 409:退款异常, 501:退款成功, 901:退票完成,
		// 909:已删除)s
		Long status = request.getStatus();
		String sta = String.valueOf(status);
		switch (sta) {
			case "252":
				// TODO 此处审核拒绝 T_REFUND_APPLY表 无相关code,暂定109;
				request.setStatus(109L);
				break;
			case "251":
				request.setStatus(201L);
				break;
			case "254":
				request.setStatus(401L);
				break;
			case "253":
				request.setStatus(3011L);
				break;
			case "255":
				request.setStatus(501L);
				break;
			default:
				break;
		}
		RefundListRes refundListRes = new RefundListRes();
		List<RefundListDto> refundList = new ArrayList<>();
		String refundType = request.getRefundType();
		try {
			switch (refundType) {
				case "REFORD":
					// 普通退款REFORD TODO @王珽此处 b2c退款类型 OTA暂定 1 | 3
					request.setBeginDate(request.getBeginDate() + " 00:00:00");
					request.setEndDate(request.getEndDate() + " 23:59:59");
					/*
					 * 0:补退款 1:退正常和改期票 2:退emd座 3:差错退款 4:退vip订单 5:退wifi订单 6:退行李订单 7退高铁 8 退citytour
					 */
					request.setRefundType("1");
					refundList = this.paymentMistakeDao.getPayMistakeAuditList(request);
					refundListRes.setRefundList(refundList);
					refundListRes.setResultCode(Constants.APP_SUCCESS_CODE);
					refundListRes.setResultMsg(Constants.APP_SUCCESS_MSG);
					break;
				case "REREFD":
					// 差错退款REREFD
					request.setBeginDate(request.getBeginDate() + " 00:00:00");
					request.setEndDate(request.getEndDate() + " 23:59:59");
					request.setRefundType("3");
					refundList = this.paymentMistakeDao.getPayMistakeAuditList(request);
					refundListRes.setRefundList(refundList);
					refundListRes.setResultCode(Constants.APP_SUCCESS_CODE);
					refundListRes.setResultMsg(Constants.APP_SUCCESS_MSG);
					break;
				default:
					refundListRes.setResultCode(Constants.APP_PARAMS_ERROR_CODE);
					refundListRes.setResultMsg(Constants.APP_PARAMS_ERROR_MSG);
					break;
			}
		} catch (Exception e) {
			refundListRes.setResultCode(Constants.APP_SERVER_ERROR_CODE);
			refundListRes.setResultMsg(Constants.APP_SERVER_ERROR_MSG);
			e.printStackTrace();
		}
		return refundListRes;
	}

	@Override
	@POST
	@Path("/appReturnNoteSearch")
	@Consumes({ "application/json" })
	@Produces(MediaType.APPLICATION_JSON)
	public ReturnNoteSearchRes returnNoteSearch(ReturnNoteSearchReq request) {
		LOG.info("调用returnNoteSearch入参 :" + GsonUtil.toJson(request));
		ReturnNoteSearchRes noteSearchRes = new ReturnNoteSearchRes();
		try {
			String ordNo = String.valueOf(request.getOrderNo());
			List<FlightOrderDO> listByOrderNo = flightOrderDao.getFlightOrderListByOrderNo(ordNo);
//		FlightOrderDO flightOrderDO = listByOrderNo.get(0);
			Set<String> tktSet = new HashSet<>();
			for (FlightOrderDO flightOrderDO : listByOrderNo) {
				Set<PaxDO> paxs = flightOrderDO.getPaxs();
				for (PaxDO paxDO : paxs) {
					Integer paxSt = paxDO.getModifySt();
					if (!paxSt.equals(3)){
						continue;
					}
					Set<TicketDO> tickets = paxDO.getTickets();
					for (TicketDO ticketDO : tickets) {
						String tktNo = ticketDO.getTktNo();
						LOG.info("退票详情票号tktNo :" + tktNo);
						tktSet.add(tktNo);
					}
				}
			}
			List<String> tktNos = new ArrayList<String>(tktSet);
			// 判空
			if (tktNos.size() == 0 || null == tktNos) {
				noteSearchRes.setResultCode(Constants.APP_PARAMS_ERROR_CODE);
				noteSearchRes.setResultMsg(Constants.APP_PARAMS_ERROR_MSG);
				LOG.info("该大订单号: " + request.getOrderNo() + "不存在退票票号");
				return noteSearchRes;
			}
			// 根据机票查询退票审核申请记录ID : apply_id
			List<Long> applyIds = refundDetailDao.getRefundApplyIdByticketId(tktNos);
			List<ReturnNoteInfo> returnNoteInfoList = new ArrayList<>();
			List<ReturnNoteOrderInfo> returnNoteOrderInfoList = new ArrayList<>();

			for (long applyId : applyIds) {
				LOG.info("退票 applyId: " + applyId);
				// 根据apply_id查询退票详情
				RefundApplyDto refundApplyDto = refundDetailDao.getRefundApplyDto(applyId);
				// 返回参数转换APP
				noteSearchRes = getRefundDetailDto(ordNo,noteSearchRes, refundApplyDto,request.getSalesChannel());
				List<ReturnNoteInfo> returnNoteInfo = noteSearchRes.getReturnNoteInfoList();
				List<ReturnNoteOrderInfo> returnNoteOrderInfo = noteSearchRes.getReturnNoteOrderInfoList();
				returnNoteInfoList.addAll(returnNoteInfo);
				returnNoteOrderInfoList.addAll(returnNoteOrderInfo);
			}
			//增加非航信息
			getAddInfo(returnNoteInfoList,tktNos,request);
			noteSearchRes.setReturnNoteInfoList(returnNoteInfoList);
			noteSearchRes.setReturnNoteOrderInfoList(returnNoteOrderInfoList);
			noteSearchRes.setResultCode(Constants.APP_SUCCESS_CODE);
			noteSearchRes.setResultMsg(Constants.APP_SUCCESS_MSG);
		} catch (Exception e) {
			LOG.error("查询退票详情异常: ", e);
			noteSearchRes.setResultCode(Constants.APP_SUCCESS_CODE);
			noteSearchRes.setResultMsg(Constants.APP_SUCCESS_MSG);
			return noteSearchRes;
		} catch (Error ex) {
			LOG.error("查询退票详情异常: ", ex);
			noteSearchRes.setResultCode(Constants.APP_SUCCESS_CODE);
			noteSearchRes.setResultMsg(Constants.APP_SUCCESS_MSG);
			return noteSearchRes;
		}

		return noteSearchRes;
	}

	private void getAddInfo(List<ReturnNoteInfo> returnNoteInfoList, List<String> tktNos, ReturnNoteSearchReq request) {
		if (CollectionUtils.isEmpty(returnNoteInfoList)){
			return;
		}
		Map<String,AncillaryListRes> ancillaryListResMap = new HashMap<>();
		for (String tkt:tktNos){
			AncillaryOrderListReq req = new AncillaryOrderListReq();
			req.setAirTicketNo(tktNos.get(0));
			List<String> types = new ArrayList<String>();
			types.add("all");//表示查询所有
			req.setTypes(types);
			req.setSalesChannel(request.getSalesChannel());
			AncillaryListRes res = null;
			try {
				res = fnEmd.searchAncillaryListByTicketInfo(req);
			}catch (Exception e){
				LOG.error("caculator additionalPrice error.",e);
				e.printStackTrace();
			}
			String resultCode = res.getResultCode();
//			调用成功
			if ("EMDSEAT0000".equals(resultCode)){
				ancillaryListResMap.put(tkt,res);
			}
		}

		if (ancillaryListResMap.isEmpty()){
			return;
		}
		Map<String,List<RefundApplyDeputyDto>> rfdDeputyMap = new HashMap<>();
		for (Map.Entry<String,AncillaryListRes> entry:ancillaryListResMap.entrySet()){
			List<String> tmpList = new ArrayList<>();
			String tktNo = entry.getKey();
			AncillaryListRes res = entry.getValue();
			List<WifiOrderDto>  wifiOrderDtoList = res.getWifiOrderDtoList();
			List<EMDOrderDto> emdOrderDtos = res.getEmdOrderDtoList();
			List<EmdBageOrderListDto> bageOrderListDtos = res.getEmdBageOrderDtoList();
			List<GaotieOrderListRes> gaotieOrderList = res.getGaotieOrderList();
			if (!CollectionUtils.isEmpty(wifiOrderDtoList)){
				for (WifiOrderDto wifiOrderDto:wifiOrderDtoList){
					String wifiOrderNo = wifiOrderDto.getWifiOrderNo();
					String orderSt = wifiOrderDto.getOrderSt();
					if (AncillaryConstant.WIFI_ORDER_ST_REF.equals(orderSt)){
						tmpList.add(wifiOrderNo);
					}
				}
			}
			if (!CollectionUtils.isEmpty(emdOrderDtos)){
				for (EMDOrderDto emdOrderDto:emdOrderDtos){
					String orderSt = emdOrderDto.getOrderSt();
					String emdOrderNo = emdOrderDto.getEmdOrderNo();
					if (AncillaryConstant.EMD_ORDER_ST_REF.equals(orderSt)){
						tmpList.add(emdOrderNo);
					}
				}
			}
			if (!CollectionUtils.isEmpty(bageOrderListDtos)){
				for (EmdBageOrderListDto bageOrderListDto:bageOrderListDtos){
					String orderSt = bageOrderListDto.getOrderST();
					String bageOrderNo = bageOrderListDto.getBageOrderNo();
					if (AncillaryConstant.BAGE_ORDER_ST_REF.equals(orderSt)){
						tmpList.add(bageOrderNo);
					}
				}
			}
			if (!CollectionUtils.isEmpty(gaotieOrderList)){
				for (GaotieOrderListRes gaotieOrderListRes:gaotieOrderList){
					String orderNo = gaotieOrderListRes.getGaotieOrderNo();
					String orderSt = gaotieOrderListRes.getOrderSt();
					if (AncillaryConstant.GAOTIE_ORDER_ST_REF.equals(orderSt)){
						tmpList.add(orderNo);
					}
				}
			}

			List<RefundApplyDeputyDto> refundApplyDeputyDtos = refundApplyDeputyDao.getRefundApplyDeputyByProductId(tmpList);
			if (!CollectionUtils.isEmpty(refundApplyDeputyDtos)){
				rfdDeputyMap.put(tktNo,refundApplyDeputyDtos);
			}
		}


		for (ReturnNoteInfo returnNoteInfo:returnNoteInfoList){
			boolean find = false;
			List<ReturnNoteAirInfo> airInfos = returnNoteInfo.getAirInfoList();
			List<AddInfo> addInfos = returnNoteInfo.getAddInfoList();
			if (addInfos == null){
				addInfos = new ArrayList<>();
				returnNoteInfo.setAddInfoList(addInfos);
			}
			for (ReturnNoteAirInfo airInfo:airInfos){
				List<ReturnNotePaxInfo> paxInfos = airInfo.getPaxInfoList();
				for (ReturnNotePaxInfo paxInfo:paxInfos){
					String tktNo = paxInfo.getTicketNo();
					List<RefundApplyDeputyDto> refundApplyDeputyDtos = rfdDeputyMap.get(tktNo);
					if (!CollectionUtils.isEmpty(refundApplyDeputyDtos)){
						for (RefundApplyDeputyDto refundApplyDeputyDto:refundApplyDeputyDtos){
							AddInfo addInfo = new AddInfo();
							String deputyType = refundApplyDeputyDto.getDeputyType();
							addInfo.setProductDetailId(refundApplyDeputyDto.getId());
							addInfo.setAddType(deputyType);
							String procName = getProcName(deputyType);
							addInfo.setProductName(procName);
							double refundAmount = refundApplyDeputyDto.getRefundAmount() == null?0:refundApplyDeputyDto.getRefundAmount().doubleValue();
							addInfo.setRefundAmount(refundAmount);
							double refundFee = refundApplyDeputyDto.getRefundFee() == null?0:refundApplyDeputyDto.getRefundFee().doubleValue();
							addInfo.setPoundageAmount(refundFee);
							Long refundPoint = refundApplyDeputyDto.getPointAmount() == null?0:refundApplyDeputyDto.getPointAmount().longValue();
							addInfo.setRefundPoint(refundPoint);
							addInfos.add(addInfo);
						}
					}
				}
			}
		}

		for (Map.Entry<String,AncillaryListRes> entry:ancillaryListResMap.entrySet()){
			String tktNo = entry.getKey();
			AncillaryListRes res = entry.getValue();
		}





	}

	private String getProcName(String deputyType) {
		String procName = "";
		switch (deputyType){
			case "10":
				procName = "WIFI";
				break;
			case "11":
				procName = "VIP";
				break;
			case "12":
				procName = "选座";
				break;
			case "13":
				procName = "行李";
				break;
			case "17":
				procName = "高铁";
				break;
			case "18":
				procName = "积分";
				break;
			default:
//				procName = "unknown";
				break;
		}

		return procName;
	}

	private ReturnNoteSearchRes getRefundDetailDto(String ordNo,ReturnNoteSearchRes noteSearchRes, RefundApplyDto refundApplyDto, String channel) {
		//查询订单详情
		ReportOrderDto order = paymentMistakeDao.getReportOrderListWithFlight(ordNo,refundApplyDto.getFlightOrderNo());
		//订单信息
		List<ReturnNoteOrderInfo> orderInfoList = new ArrayList<>();
		ReturnNoteOrderInfo noteOrderInfo = new ReturnNoteOrderInfo();
		noteOrderInfo.setOrderNo(Long.parseLong(ordNo));
		//返回外币
		noteOrderInfo.setTotalPayAmount(order.getOrderScny().doubleValue());//人民币CNY
		noteOrderInfo.setTotalPayPoint(Long.valueOf(order.getPointAmount()));
		//noteOrderInfo.setCouponInfo();优惠券信息
		//支付状态 10：等待支付   20：支付中  	30：支付失败
		//         40：支付成功   50：差错退款 	60：风控人工审核中

		//APP  10050: 等待支付;10051:支付成功;10052:支付异常;10053:差错退款;10054:交易成功;10055:交易异常
		//TODO 此处需要 @王珽 统一code TODO 免费改期 头等舱/公务舱等高仓位 无支付信息,默认支付成功
		String paySt = order.getPaySt();
		if (paySt == null || "".equals(paySt)) {
			noteOrderInfo.setOrderStatus("40");
		}else {
			noteOrderInfo.setOrderStatus(paySt);
		}
		noteOrderInfo.setCreateDate(order.getCreateDtm());
		noteOrderInfo.setOrderType(order.getOrderTp());
		noteOrderInfo.setPaxNum(order.getPaxCount().longValue());
		orderInfoList.add(noteOrderInfo);

		List<ReturnNoteInfo> returnNoteInfoList = new ArrayList<>();
		ReturnNoteInfo noteInfo = new ReturnNoteInfo();
		noteInfo.setReturnNoteNo(refundApplyDto.getId());
		//支付信息
		List<ReturnNotePayInfo> payInfoList = new ArrayList<>();
		ReturnNotePayInfo payInfo = new ReturnNotePayInfo();
		payInfo.setBankCode(order.getBankCode());
		//payInfo.setBankGate();银行网关
		payInfo.setBankSerial(order.getPaySuccessNo());
		payInfoList.add(payInfo);
		noteInfo.setPayInfoList(payInfoList);//支付信息

		//机票信息
		List<ReturnNoteAirInfo> airInfoList = new ArrayList<>();
		ReturnNoteAirInfo noteAirInfo = new ReturnNoteAirInfo();
		//乘客信息
		List<ReturnNotePaxInfo> paxInfoList = new ArrayList<>();
		ReturnNotePaxInfo returnNotePaxInfo = new ReturnNotePaxInfo();
		RefundTktDto refundTkt = refundApplyDto.getRefundTkt();
		returnNotePaxInfo.setPaxId(refundTkt.getPaxId());
		returnNotePaxInfo.setPaxName(refundTkt.getPaxName());
		returnNotePaxInfo.setPaxType(refundTkt.getPaxType());
		returnNotePaxInfo.setIdType(order.getPassTp());
		returnNotePaxInfo.setIdNo(order.getPassNo());
		returnNotePaxInfo.setTicketNo(refundTkt.getTicketNo());
		returnNotePaxInfo.setInfCarrierName(refundTkt.getPaxName());
		//航段信息
		List<ReturnNoteSegInfo> segInfoList = new ArrayList<>();
		Set<RefundSegDto> refundSegs = refundTkt.getRefundSegs();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (RefundSegDto refundSeg : refundSegs) {
			ReturnNoteSegInfo returnNoteSegInfo = new ReturnNoteSegInfo();
			//returnNoteSegInfo.setMarketCarrier(refundSeg.get);
			returnNoteSegInfo.setMarketFlightNo(refundSeg.getFlightNo());

			returnNoteSegInfo.setDepartDate((refundSeg.getDepartDate()==null)?null:sdf.format(refundSeg.getDepartDate()));
			returnNoteSegInfo.setArriveDate((refundSeg.getArriveDate()==null)?null:sdf.format(refundSeg.getArriveDate()));
			List<String> airPorts = new ArrayList<>();
			airPorts.add(refundSeg.getDepartAirport());
			airPorts.add(refundSeg.getArriveAirport());
			ChannelManageDto channelManageDto = refundLocationService.findChannelByCode(channel);
			String language = channelManageDto.getLanguageCode();
			Map<String, AirportDto> nationCityAirportDtoMap = refundLocationService.findNationCityAirNameByCodes(airPorts);
			AirportDto airportInfoDep = nationCityAirportDtoMap.get(refundSeg.getDepartAirport());
			AirportDto airportInfoArr = nationCityAirportDtoMap.get(refundSeg.getArriveAirport());
			returnNoteSegInfo.setDepartAirPort(airportInfoDep.getName(language));
			returnNoteSegInfo.setArriveAirPort(airportInfoArr.getName(language));
			returnNoteSegInfo.setTktSegStatus(refundSeg.getStatus());
			returnNoteSegInfo.setEiCommont(refundSeg.getEiContent());
			returnNoteSegInfo.setTktNo(refundTkt.getTicketNo());
			returnNoteSegInfo.setPayAmount(refundSeg.getRefundTkt().getTktAmt().doubleValue());
			returnNoteSegInfo.setPayPoint(refundSeg.getRefundTkt().getTktPoints().doubleValue());
			returnNoteSegInfo.setPoundageAmount(refundSeg.getRefundTkt().getRefundFee().doubleValue());
			returnNoteSegInfo.setPoundagePoint(refundSeg.getRefundTkt().getRefundPoints().doubleValue());
			returnNoteSegInfo.setRefundAmount(refundSeg.getRefundTkt().getTotalRefundAmt().doubleValue());
			returnNoteSegInfo.setRefundPoint(refundSeg.getRefundTkt().getRefundPoints().doubleValue());
			//应退卡券信息(暂缺)
			List<CouponInfo> couponInfoList = new ArrayList<>();
			CouponInfo couponInfo = new CouponInfo();
			couponInfoList.add(couponInfo);
			returnNoteSegInfo.setRefundCouponInfoList(couponInfoList);

			segInfoList.add(returnNoteSegInfo);
		}
		returnNotePaxInfo.setSegInfoList(segInfoList);

		paxInfoList.add(returnNotePaxInfo);
		noteAirInfo.setPaxInfoList(paxInfoList);
		airInfoList.add(noteAirInfo);
		noteInfo.setAirInfoList(airInfoList);//机票信息
		//附加信息(暂缺) TODO @王珽
		List<AddInfo> addInfoList = new ArrayList<>();
		AddInfo addInfo = new AddInfo();
		noteInfo.setAddInfoList(addInfoList);//附加信息

		noteInfo.setTotalRefundAmount(refundApplyDto.getRefundAmount().doubleValue());
		noteInfo.setTotalRefundPoint(refundApplyDto.getRefundPoints().doubleValue());
		noteInfo.setRefundType(refundApplyDto.getClaimReason());
		noteInfo.setRefundApplyDate((refundApplyDto.getCreatDate()==null)?null:sdf.format(refundApplyDto.getCreatDate()));
		noteInfo.setRefundApplyPerson(refundApplyDto.getClaimerName());
		noteInfo.setRefundApplyPerTel(refundApplyDto.getClaimerPhone());
		noteInfo.setRefundReason(refundApplyDto.getClaimReason());
		//状态(等待审核(等待一审),等待用户更新,一审异常,等待二审,等待重新审核,退票单异常,,等待退款,退款成功,退款异常)
		noteInfo.setAuditingStatus(refundApplyDto.getStatus());
		//审核信息
		List<AuditingInfo> auditingInfoList = new ArrayList<>();
		Set<RefundAuditDto> refundAudits = refundApplyDto.getRefundAudits();
		if (!CollectionUtils.isEmpty(refundAudits)){
			List<RefundAuditDto> refundAuditDtoList = new ArrayList<>(refundAudits);
			Collections.sort(refundAuditDtoList, new Comparator<RefundAuditDto>() {
				@Override
				public int compare(RefundAuditDto o1, RefundAuditDto o2) {
					if (o1.getAuditDate().after(o2.getAuditDate())){
						return -1;
					}
					return 1;
				}
			});
			for (RefundAuditDto refundAudit : refundAuditDtoList) {
				AuditingInfo auditingInfo = new AuditingInfo();
				//auditingInfo.getAddType();子业务类型
				//审核阶段(100:申请, 200:一审, 300:二审, 400:退款)
				String auditPhase = refundAudit.getAuditPhase();
				switch (auditPhase){
					case "100":
						auditingInfo.setAddName("退票申请");
						break;
					case "200":
						auditingInfo.setAddName("一审");
						break;
					case "300":
						auditingInfo.setAddName("二审");
						break;
					case "400":
						auditingInfo.setAddName("退款");
						break;
				}

				//是否通过(0:拒绝; 1:通过;2:审核出错)  TODO
				long auditStatus = Long.valueOf(refundAudit.getAuditStatus());
				auditingInfo.setAuditingStatus(auditStatus);
				auditingInfo.setAuditTime(DateUtil.toString(refundAudit.getAuditDate(),DateUtil.yyyy_MM_dd_HH_mm));
				auditingInfo.setAuditPerson(refundAudit.getAuditorName());
				auditingInfo.setAuditingOpinion(refundAudit.getAuditDesc());
				auditingInfo.setAuditingRemark(refundAudit.getAuditDesc());
				auditingInfoList.add(auditingInfo);
			}
			noteInfo.setAuditingInfoList(auditingInfoList);//审核信息
		}

		//附件信息
		List<EnclosureInfo> enclosureInfoList = new ArrayList<>();
		Set<RefundAttachmentDto> refundAttachments = refundApplyDto.getRefundAttachments();
		for (RefundAttachmentDto refundAttachmentDto : refundAttachments) {
			EnclosureInfo enclosureInfo = new EnclosureInfo();
			enclosureInfo.setEnclosureId(refundAttachmentDto.getId());
			enclosureInfo.setEnclosureName(refundAttachmentDto.getContentDesc());
			enclosureInfoList.add(enclosureInfo);
		}
		noteInfo.setEnclosureInfoList(enclosureInfoList);//附件信息
		returnNoteInfoList.add(noteInfo);//退货单信息
		noteSearchRes.setReturnNoteInfoList(returnNoteInfoList);
		noteSearchRes.setReturnNoteOrderInfoList(orderInfoList);//订单信息
		return noteSearchRes;
	}

	@Override
	@POST
	@Path("/orderDetailSearch")
	@Consumes({ "application/json" })
	@Produces(MediaType.APPLICATION_JSON)
	public OrderDetailSearchRes orderDetailSearch(OrderDetailSearchReq request) {
		LOG.info("调用orderDetailSearch入参 :" + GsonUtil.toJson(request));
		OrderDetailSearchRes detailSearchRes = new OrderDetailSearchRes();
		List<DruidPaxInfo> paxList = new ArrayList<>();
		String ffpNo = "";//常旅客卡号
		try {
			String ordNo = String.valueOf(request.getOrderNo());
			List<FlightOrderDO> listByOrderNo = flightOrderDao.getFlightOrderListByOrderNo(ordNo);
			detailSearchRes = getReturnNoteInfos(ordNo,detailSearchRes,listByOrderNo);

			Set<String> tktSet = new HashSet<>();
			Map<String,Set<String>> paramMap = new HashMap<>();
			List<SubOrderInfo> subOrderInfos = detailSearchRes.getSubOrderInfoByOrderList();
			for (FlightOrderDO flightOrderDO : listByOrderNo) {
				Integer orderStatus = flightOrderDO.getOrderSt();
				if (!Objects.equals(FlightOrderStatusEnum.TICKETED.getCode(), orderStatus)) {
					continue;
				}
				Set<PaxDO> paxs = flightOrderDO.getPaxs();
				if (CollectionUtils.isEmpty(paxs)){
					continue;
				}
				Set<String> tktNoSet = new HashSet<>();
				List<Long> paxIdList = new ArrayList<>();
				Date tktDate = null;
				for (PaxDO pax : paxs) {
					if (StringUtils.isEmpty(ffpNo)){
						if(!StringUtils.isEmpty(pax.getFfp())){
							ffpNo = pax.getFfp();
						}
					}
					if(tktDate == null){
						Set<TicketDO> ticketDOS = pax.getTickets();
						if (CollectionUtils.isEmpty(ticketDOS)){
							LOG.error("can't find ticket,paxId:{},flightOrderNo:{}",pax.getPassengerId(),flightOrderDO.getFlightOrderNo());
							detailSearchRes.setResultCode(Constants.APP_PARAMS_ERROR_CODE);
							detailSearchRes.setResultMsg(Constants.APP_SERVER_ERROR_MSG);
							return detailSearchRes;
						}
						tktDate = ticketDOS.iterator().next() .getTktDate();
					}
					paxIdList.add(pax.getPassengerId());
					DruidPaxInfo druidPaxInfo = new DruidPaxInfo();
					druidPaxInfo.setPointTp(pax.getPointTp());
					druidPaxInfo.setPaxId(pax.getPassengerId());
					druidPaxInfo.setPaxName(pax.getPaxFullName());
					druidPaxInfo.setPaxType(pax.getPassengerTp());
					druidPaxInfo.setIdType(pax.getPassTp());
					druidPaxInfo.setIdNo(pax.getPassNo());
					druidPaxInfo.setTicketNo(pax.getTktNo());
					druidPaxInfo.setFlightOrderNo(pax.getFlightOrderNo());
					//druidPaxInfo.setProductType(productType);产品类型
					//druidPaxInfo.setFareSource(fareSource);运价源
					druidPaxInfo.setModifySt(pax.getModifySt());
					druidPaxInfo.setInfCarrierName(pax.getGuardianName());
					druidPaxInfo.setBirthday(pax.getPassengerBirthday());
					Map<String, Double> extendTaxMap = new HashMap<>();
					List<DruidSegInfo> segInfoList = new ArrayList<>();
					Set<SegmentDO> segments = pax.getSegments();
					long isInter = 0;
					for (SegmentDO segment : segments) {
						if(segment.getIsinter()){
							isInter = 1;
						}
						Set<TaxFeeDO> taxFees = segment.getTaxFees();
						for (TaxFeeDO taxFee : taxFees) {
							extendTaxMap.put(taxFee.getTaxTp(), taxFee.getTaxAm().doubleValue());
						}
						DruidSegInfo druidSegInfo = new DruidSegInfo();
						druidSegInfo.setSegId(segment.getSegId());
						druidSegInfo.setSegIndex((long)segment.getSegSq());
						druidSegInfo.setMarketCarrier(segment.getCarrCd());
						druidSegInfo.setOperateCarrier(segment.getRealCarrCd());
						druidSegInfo.setMarketFlightNo(segment.getFlightNo());
						druidSegInfo.setOperateFlightNo(segment.getRealFlightNo());
						druidSegInfo.setDepartDate(DateUtil.toString(segment.getDepDateAndTime(),DateUtil.yyyy_MM_dd_HH_mm));
						druidSegInfo.setArriveDate(DateUtil.toString(segment.getArrDateAndTime(),DateUtil.yyyy_MM_dd_HH_mm));
						druidSegInfo.setDepartAirPort(segment.getDepAirpCd());
						druidSegInfo.setArriveAirPort(segment.getArrAirpCd());
						//三字码转机场名
						List<String> airPorts = new ArrayList<>();
						airPorts.add(segment.getDepAirpCd());
						airPorts.add(segment.getArrAirpCd());

						ChannelManageDto channelManageDto = refundLocationService.findChannelByCode(request.getSalesChannel());
						String language = channelManageDto.getLanguageCode();
						Map<String, AirportDto> nationCityAirportDtoMap = refundLocationService.findNationCityAirNameByCodes(airPorts);
						AirportDto airportInfoDep = nationCityAirportDtoMap.get(segment.getDepAirpCd());
						AirportDto airportInfoArr = nationCityAirportDtoMap.get(segment.getArrAirpCd());
						druidSegInfo.setDepartAirPortName(airportInfoDep.getName(language));
						druidSegInfo.setArriveAirPortName(airportInfoArr.getName(language));

						druidSegInfo.setTktSegStatus(segment.getSegSt());
						druidSegInfo.setTktSegStatusName(segment.getSegSt());
						//druidSegInfo.setTigMsgCn(tigMsgCn);//不可退时展示给旅客信息
						//druidSegInfo.setTigMsgEn(tigMsgCn);
						druidSegInfo.setEiCommont(segment.getEcomment());
						druidSegInfo.setTktNo(pax.getTktNo());
						druidSegInfo.setCabin(segment.getClassTp());
						druidSegInfo.setPayAmt(segment.getScny().doubleValue());
						druidSegInfo.setPayCurrency(segment.getCurrency());
						//druidSegInfo.setAirPortTax(airPortTax);基建税
						//druidSegInfo.setFuelTax(fuelTax);燃油税
						druidSegInfo.setRefundable(false);
						segInfoList.add(druidSegInfo);
					}
					druidPaxInfo.setIsInter(isInter);
					druidPaxInfo.setExtendTaxMap(extendTaxMap);
					druidPaxInfo.setSegInfoList(segInfoList);
					String tktNo = pax.getTktNo();
					paxList.add(druidPaxInfo);
					if (2 == pax.getModifySt()){
						tktNoSet.add(tktNo);
						tktSet.add(tktNo);
					}
				}
				if (!CollectionUtils.isEmpty(tktNoSet)){
					paramMap.put(flightOrderDO.getFlightOrderNo(),tktNoSet);
				}
				for (SubOrderInfo subOrderInfo:subOrderInfos){
					String subId = subOrderInfo.getSubId();
					if (subId.equals(flightOrderDO.getFlightOrderNo())){
						subOrderInfo.setPaxIdList(paxIdList);
						subOrderInfo.setTktDate(tktDate);
					}
				}
			}
			detailSearchRes.setPaxList(paxList);
			detailSearchRes.setFfpNo(ffpNo);
			List<String> tktNos = new ArrayList<String>(tktSet);
			// 判空
			if (tktNos.size() == 0 || null == tktNos) {
				queryPaxApplyStatus(detailSearchRes);
				boolean isRefund = false;
				List<DruidPaxInfo> paxInfos = detailSearchRes.getPaxList();
				for (DruidPaxInfo paxInfo:paxList){
					Long applyId = paxInfo.getRefundApplyId();
					if (applyId !=null && applyId >0){
						isRefund = true;
						break;
					}
				}
				if (isRefund){
					detailSearchRes.setResultCode(Constants.APP_SUCCESS_CODE);
					detailSearchRes.setResultMsg(Constants.APP_SUCCESS_MSG);
					return detailSearchRes;
				}
				detailSearchRes = new OrderDetailSearchRes();
				detailSearchRes.setResultCode(Constants.APP_PARAMS_ERROR_CODE);
				detailSearchRes.setResultMsg(Constants.APP_PARAMS_ERROR_MSG);
				LOG.info("该大订单号: " + request.getOrderNo() + "未出票");
				return detailSearchRes;
			}
			//查询票详情
			for (FlightOrderDO flightOrderDO : listByOrderNo) {
				Integer orderStatus = flightOrderDO.getOrderSt();
				if (!Objects.equals(FlightOrderStatusEnum.TICKETED.getCode(), orderStatus)) {
					continue;
				}
				// 返回参数转换APP
				String flightOrderNo = flightOrderDO.getFlightOrderNo();
				Set<String> tktStrSet = paramMap.get(flightOrderNo);
				if (CollectionUtils.isEmpty(tktStrSet)){
					continue;
				}
				if (tktStrSet.contains(null)){
					tktStrSet.remove(null);
				}

				RefundableResultList refundableResultList = refundLocationService.checkRefundable(flightOrderNo,tktStrSet,request.getCreateId(),request.getFfpNo());
				if (refundableResultList == null){
					LOG.error("查询退票详情异常,flightOrderNo:{},paramSet:{}",flightOrderNo,tktStrSet);
					detailSearchRes = new OrderDetailSearchRes();
					detailSearchRes.setResultCode(Constants.APP_PARAMS_ERROR_CODE);
					detailSearchRes.setResultMsg(Constants.APP_SERVER_ERROR_MSG);
					return detailSearchRes;
				}
				for (DruidPaxInfo druidPaxInfo:paxList){
					String tktNo = druidPaxInfo.getTicketNo();
					List<DruidSegInfo> segInfos = druidPaxInfo.getSegInfoList();
					RefundableResult refundableResult = refundableResultList.getSingleResult(tktNo);
					if (refundableResult == null){
						continue;
					}
					if(refundableResult.isRefundableInPolicy()){
						int idx = refundableResult.getRefundableIndex();
						for (DruidSegInfo segInfo:segInfos){
							long segIdx = segInfo.getSegIndex();
							if (segIdx>=idx){
								segInfo.setRefundable(true);
							}else {
								segInfo.setRefundable(false);
							}
						}
					}
				}
			}
			detailSearchRes.setResultCode(Constants.APP_SUCCESS_CODE);
			detailSearchRes.setResultMsg(Constants.APP_SUCCESS_MSG);

			//20190308增加乘客退票申请状态
			queryPaxApplyStatus(detailSearchRes);
		} catch (Exception e) {
			LOG.error("查询退票详情异常: ", e);
			detailSearchRes = new OrderDetailSearchRes();
			detailSearchRes.setResultCode(Constants.APP_PARAMS_ERROR_CODE);
			detailSearchRes.setResultMsg(Constants.APP_SERVER_ERROR_MSG);
			return detailSearchRes;
		} catch (Error ex) {
			LOG.error("查询退票详情异常: ", ex);
			detailSearchRes = new OrderDetailSearchRes();
			detailSearchRes.setResultCode(Constants.APP_PARAMS_ERROR_CODE);
			detailSearchRes.setResultMsg(Constants.APP_SERVER_ERROR_MSG);
			return detailSearchRes;
		}
		return detailSearchRes;
	}

	private void queryPaxApplyStatus(OrderDetailSearchRes detailSearchRes) {
		List<DruidPaxInfo> paxInfos = detailSearchRes.getPaxList();
		List<String> tktNoList = new ArrayList<>();
		for (DruidPaxInfo paxInfo:paxInfos){
			Integer modifySt = paxInfo.getModifySt();
			if (3==modifySt){
				RefundApplyDto refundApply = refundDetailDao.getRefundApplyStatusByTicketNo(paxInfo.getTicketNo());
				String refundApplyStatus = refundApply.getStatus();
				paxInfo.setRefundApplyId(refundApply.getId());
				if ("2011".equals(refundApplyStatus)){
					paxInfo.setRefundStatus(refundApplyStatus);
				}
			}
		}
	}

	private OrderDetailSearchRes getReturnNoteInfos(String ordNo, OrderDetailSearchRes detailSearchRes,List<FlightOrderDO> flightOrderDOList) {
		//查询订单详情
		OrderDO order = orderDao.getOrderDetail(ordNo);
		detailSearchRes.setOrderNo(Long.valueOf(ordNo));
		detailSearchRes.setOrigChannel(order.getChannel());
		String createDtm = DateUtil.toString(order.getCreateDtm(),DateUtil.yyyy_MM_dd_HH_mm_ss);
		detailSearchRes.setCreateDate(createDtm);
		detailSearchRes.setOrderType("BIG_AIR");
//	    detailSearchRes.setFfpNo(order.getFfp()); //常客卡号
		//detailSearchRes.setCompanyCode(order.getProductCode());//结算类型
//		detailSearchRes.setPaxNum(order.getPaxCount());
		//detailSearchRes.setStrengthRelation(strengthRelation);//强弱关系JSON

		Set<PayDO> payListByOrderNo = paymentMistakeDao.getPayListByOrderNo(ordNo);
		List<SubOrderInfo> subOrderInfoByOrderList = new ArrayList<>();
		List<String> subOrderIdList = new ArrayList<>();
		for (PayDO payDO : payListByOrderNo) {
			int paySt = payDO.getPaySt();
			//判断是否支付成功
			if(!Objects.equals(40,paySt)){
				continue;
			}
			detailSearchRes.setPayTp(payDO.getPccode());
			SubOrderInfo subOrderInfo = new SubOrderInfo();
			subOrderInfo.setSubOrderStatus(payDO.getPaySt()+"");
			Set<PayProductDO> payProducts = payDO.getPayProducts();
			for (PayProductDO payProduct : payProducts) {
				subOrderInfo.setOrderNo(ordNo);
				subOrderInfo.setSubId(payProduct.getProductId());
				//subOrderInfo.setServiceNo(payProduct.getProductId());//例如保险保单号，附加服务的emd号之类
				subOrderInfo.setSubOrderNo(payProduct.getProductId());
				subOrderInfo.setSubOrderType(payProduct.getProductType()+"");
				subOrderInfo.setSubOrderProduct(payProduct.getProductType()+"");
				subOrderInfo.setSubOrderTp(subOrderInfo.getSubOrderType());
				//subOrderInfo.setTigMsgCn(tigMsgCn);	中文提示文案
				//subOrderInfo.setTigMsgEn(tigMsgCn);
				//subOrderInfo.isRefundable();是否可退
				//subOrderInfo.setSubOrderDetail(subOrderDetail);子订单明细特殊属性
				List<DruidPayInfo> subPayInfoList = new ArrayList<>();
				DruidPayInfo druidPayInfo = new DruidPayInfo();
				druidPayInfo.setAmount(payProduct.getPayAmount().doubleValue());
				druidPayInfo.setCurrency(payDO.getCurr());
				subPayInfoList.add(druidPayInfo);
				subOrderInfo.setSubPayInfoList(subPayInfoList);
				subOrderInfoByOrderList.add(subOrderInfo);
				subOrderIdList.add(subOrderInfo.getSubId());
			}
		}

		//20190410增加免费改期机票订单
		for (FlightOrderDO flightOrderDO : flightOrderDOList) {
			Integer orderStatus = flightOrderDO.getOrderSt();
			if (!Objects.equals(FlightOrderStatusEnum.TICKETED.getCode(), orderStatus)) {
				continue;
			}
			String flightOrderNo = flightOrderDO.getFlightOrderNo();
			if (subOrderIdList.contains(flightOrderNo)){
				continue;
			}
			int orderTp = flightOrderDO.getOrderTp();//订单性质 1原始订单2升舱订单3收费改期订单*
			SubOrderInfo subOrderInfo = new SubOrderInfo();
			subOrderInfo.setSubOrderStatus("40");
			subOrderInfo.setOrderNo(ordNo);
			subOrderInfo.setSubId(flightOrderNo);
			subOrderInfo.setSubOrderNo(flightOrderNo);
			String subOrderType = "";
			if (orderTp == 1){
				subOrderType = "1";
			}else {
				subOrderType = "3";
			}
			subOrderInfo.setSubOrderType(subOrderType);
			subOrderInfo.setSubOrderProduct(subOrderType);
			subOrderInfo.setSubOrderTp(subOrderInfo.getSubOrderType());
			List<DruidPayInfo> subPayInfoList = new ArrayList<>();
			DruidPayInfo druidPayInfo = new DruidPayInfo();
			druidPayInfo.setAmount(flightOrderDO.getScNy().doubleValue());
			druidPayInfo.setCurrency(flightOrderDO.getCurrency());
			subPayInfoList.add(druidPayInfo);
			subOrderInfo.setSubPayInfoList(subPayInfoList);
			subOrderInfoByOrderList.add(subOrderInfo);
			subOrderIdList.add(subOrderInfo.getSubId());

		}


		detailSearchRes.setSubOrderInfoByOrderList(subOrderInfoByOrderList );
//		List<RelateOrderDetail> relateOrderDetail = new ArrayList<>();
//		RelateOrderDetail relateOrders = new RelateOrderDetail();
//		relateOrders.setOrderNo(Long.valueOf(ordNo));
//		relateOrders.setCreateDate(createDtm);
//		//TODO @王珽 确认 暂定机票
//		relateOrders.setOrderType("AIR");//订单类型 ; AIR、INS、SEAT等沿用子订单类型;交叉销售:XS
////		relateOrders.setFfpNo(order.getFfp());
//		relateOrders.setCompanyCode("MUCO");//MUCO:股份公司;MUOL:电商公司
//		relateOrderDetail.add(relateOrders);
//		detailSearchRes.setRelateOrderDetail(relateOrderDetail );
		return detailSearchRes;
	}

	private OrderDetailSearchRes getReturnNoteInfoList(String ordNo, OrderDetailSearchRes detailSearchRes,
													   RefundApplyDto refundApplyDto, String channel) {
		//查询订单详情
		ReportOrderDto order = paymentMistakeDao.getReportOrderListWithFlight(ordNo,refundApplyDto.getFlightOrderNo());
		detailSearchRes.setOrderNo(Long.valueOf(ordNo));
		detailSearchRes.setCreateDate(order.getCreateDtm());

		String paySt = order.getPaySt();
		if (paySt == null || "".equals(paySt)) {
			detailSearchRes.setOrderStatus("10051");
		}else{
			switch (paySt) {
				case "10":
					detailSearchRes.setOrderStatus("10050");
					break;
				case "20":
					detailSearchRes.setOrderStatus("10050");
					break;
				case "30":
					detailSearchRes.setOrderStatus("10052");
					break;
				case "40":
					detailSearchRes.setOrderStatus("10051");
					break;
				case "50":
					detailSearchRes.setOrderStatus("10053");
					break;
				case "60":
					detailSearchRes.setOrderStatus("10055");
					break;
				default:
					break;
			}
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		detailSearchRes.setOrderType(order.getOrderTp());
		detailSearchRes.setFfpNo(order.getFfp()); //常客卡号
		//detailSearchRes.setCompanyCode(order.getProductCode());//结算类型
		detailSearchRes.setPaxNum(order.getPaxCount());
		//detailSearchRes.setStrengthRelation(strengthRelation);//强弱关系JSON
		detailSearchRes.setApplyDate(((refundApplyDto.getCreatDate() == null)? null:sdf.format(refundApplyDto.getCreatDate())));
		detailSearchRes.setPhone(refundApplyDto.getClaimerPhone());
		detailSearchRes.setApplyPerson(refundApplyDto.getClaimerName());
		Set<PayDO> payListByOrderNo = paymentMistakeDao.getPayListByOrderNo(ordNo);
		List<SubOrderInfo> subOrderInfoByOrderList = new ArrayList<>();
		for (PayDO payDO : payListByOrderNo) {
			Set<PayProductDO> payProducts = payDO.getPayProducts();
			for (PayProductDO payProduct : payProducts) {
				SubOrderInfo subOrderInfo = new SubOrderInfo();
				subOrderInfo.setSubOrderStatus(payDO.getPaySt()+"");
				subOrderInfo.setOrderNo(ordNo);
				subOrderInfo.setSubId(payProduct.getProductId());
				//subOrderInfo.setServiceNo(payProduct.getProductId());//例如保险保单号，附加服务的emd号之类
				subOrderInfo.setSubOrderNo(payProduct.getProductId());
				subOrderInfo.setSubOrderType(payProduct.getProductType()+"");
				subOrderInfo.setSubOrderProduct(payProduct.getProductType()+"");
				//subOrderInfo.setTigMsgCn(tigMsgCn);	中文提示文案
				//subOrderInfo.setTigMsgEn(tigMsgCn);
				//subOrderInfo.isRefundable();是否可退
				//subOrderInfo.setSubOrderDetail(subOrderDetail);子订单明细特殊属性
				List<DruidPayInfo> subPayInfoList = new ArrayList<>();
				DruidPayInfo druidPayInfo = new DruidPayInfo();
				druidPayInfo.setAmount(payProduct.getPayAmount().doubleValue());
				druidPayInfo.setCurrency(order.getPromoCurrency());
				subPayInfoList.add(druidPayInfo);
				subOrderInfo.setSubPayInfoList(subPayInfoList);
				subOrderInfoByOrderList.add(subOrderInfo);
			}
		}

		detailSearchRes.setSubOrderInfoByOrderList(subOrderInfoByOrderList );
		//乘客
		List<DruidPaxInfo> paxList = new ArrayList<>();
		List<PaxDO> paxs = flightDao.getByFlightOrderPaXNo(order.getFlightOrderNo());
		for (PaxDO pax : paxs) {
			DruidPaxInfo druidPaxInfo = new DruidPaxInfo();
			druidPaxInfo.setPaxId(pax.getPassengerId());
			druidPaxInfo.setPaxName(pax.getPaxFullName());
			druidPaxInfo.setPaxType(pax.getPassengerTp());
			druidPaxInfo.setIdType(pax.getPassTp());
			druidPaxInfo.setIdNo(pax.getPassNo());
			druidPaxInfo.setTicketNo(pax.getTktNo());
			//druidPaxInfo.setProductType(productType);产品类型
			//druidPaxInfo.setFareSource(fareSource);运价源
			//druidPaxInfo.setIsInter(isInter);国际国内
			druidPaxInfo.setInfCarrierName(pax.getGuardianName());
			druidPaxInfo.setBirthday(pax.getPassengerBirthday());
			Map<String, Double> extendTaxMap = new HashMap<>();
			List<DruidSegInfo> segInfoList = new ArrayList<>();
			Set<SegmentDO> segments = pax.getSegments();
			for (SegmentDO segment : segments) {
				Set<TaxFeeDO> taxFees = segment.getTaxFees();
				for (TaxFeeDO taxFee : taxFees) {
					extendTaxMap.put(taxFee.getTaxTp(), taxFee.getTaxAm().doubleValue());
				}
				DruidSegInfo druidSegInfo = new DruidSegInfo();
				druidSegInfo.setSegId(segment.getSegId());
				druidSegInfo.setSegIndex((long)segment.getSegSq());
				druidSegInfo.setMarketCarrier(segment.getCarrCd());
				druidSegInfo.setMarketFlightNo(segment.getFlightNo());
				druidSegInfo.setOperateFlightNo(segment.getRealFlightNo());
				druidSegInfo.setDepartDate(sdf.format(segment.getDepDateAndTime()));
				druidSegInfo.setArriveDate(sdf.format(segment.getArrDateAndTime()));
				druidSegInfo.setDepartAirPort(segment.getDepAirpCd());
				druidSegInfo.setArriveAirPort(segment.getArrAirpCd());
				//三字码转机场名
				List<String> airPorts = new ArrayList<>();
				airPorts.add(segment.getDepAirpCd());
				airPorts.add(segment.getArrAirpCd());
				ChannelManageDto channelManageDto = refundLocationService.findChannelByCode(channel);
				String language = channelManageDto.getLanguageCode();
				Map<String, AirportDto> nationCityAirportDtoMap = refundLocationService.findNationCityAirNameByCodes(airPorts);
				AirportDto airportInfoDep = nationCityAirportDtoMap.get(segment.getDepAirpCd());
				AirportDto airportInfoArr = nationCityAirportDtoMap.get(segment.getArrAirpCd());
				druidSegInfo.setDepartAirPortName(airportInfoDep.getName(language));
				druidSegInfo.setArriveAirPortName(airportInfoArr.getName(language));

				druidSegInfo.setTktSegStatus(segment.getSegSt());
				druidSegInfo.setTktSegStatusName(segment.getSegSt());
				//druidSegInfo.setTigMsgCn(tigMsgCn);//不可退时展示给旅客信息
				//druidSegInfo.setTigMsgEn(tigMsgCn);
				druidSegInfo.setEiCommont(segment.getEcomment());
				druidSegInfo.setTktNo(String.valueOf(segment.getTktID()));
				druidSegInfo.setCabin(segment.getClassTp());
				druidSegInfo.setPayAmt(segment.getScny().doubleValue());
				druidSegInfo.setPayCurrency(segment.getCurrency());
				//druidSegInfo.setAirPortTax(airPortTax);基建税
				//druidSegInfo.setFuelTax(fuelTax);燃油税
				//0:退票前原始 ; 1:已退票 ; 2:退票失败
				String refundStatus = segment.getSegSt();
				druidSegInfo.setRefundable(("REFUNDED".equals(refundStatus) )?true : false);
				segInfoList.add(druidSegInfo);
			}
			druidPaxInfo.setExtendTaxMap(extendTaxMap);
			druidPaxInfo.setSegInfoList(segInfoList);
			paxList.add(druidPaxInfo);
		}
		detailSearchRes.setPaxList(paxList);
		List<RelateOrderDetail> relateOrderDetail = new ArrayList<>();
		RelateOrderDetail relateOrders = new RelateOrderDetail();
		relateOrders.setOrderNo(Long.valueOf(ordNo));
		relateOrders.setCreateDate(order.getCreateDtm());
		if (paySt == null || "".equals(paySt)) {
			relateOrders.setOrderStatus("10051");
		}else{
			switch (paySt) {
				case "10":
					relateOrders.setOrderStatus("10050");
					break;
				case "20":
					relateOrders.setOrderStatus("10050");
					break;
				case "30":
					relateOrders.setOrderStatus("10052");
					break;
				case "40":
					relateOrders.setOrderStatus("10051");
					break;
				case "50":
					relateOrders.setOrderStatus("10053");
					break;
				case "60":
					relateOrders.setOrderStatus("10055");
					break;
				default:
					break;
			}
		}

		//TODO @王珽 确认 暂定机票
		relateOrders.setOrderType("AIR");//订单类型 ; AIR、INS、SEAT等沿用子订单类型;交叉销售:XS
		relateOrders.setFfpNo(order.getFfp());
		relateOrders.setCompanyCode("MUCO");//MUCO:股份公司;MUOL:电商公司
		relateOrderDetail.add(relateOrders);
		detailSearchRes.setRelateOrderDetail(relateOrderDetail );
		return detailSearchRes;
	}
}
